﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using Gx.Common;
using Gx.Dal.Role;
using Gx.Model;
using Gx.Model.Enums;
using Gx.Model.Other;
using Gx.Model.Role;

namespace Gx.Bll.Role
{
    /// <summary>
    /// 页面路径逻辑层
    /// </summary>
    public class M_PageUrlInfoBll
    {
        /// <summary>
        /// 得到页面信息树形结构 PageUrlInfo/Index
        /// </summary>
        /// <returns></returns>
        public List<M_PageUrlInfo> GetPageUrlJson()
        {
            List<M_PageUrlInfo> list = new M_PageUrlInfoBll().GetPageUrlInfoList();
            List<M_PageUrlInfo> oneList = list.Where(x => x.LevelID == ((int)PageLevel.一级).ToString()).ToList();

            foreach (var one in oneList)
            {
                one.state = "closed";
                one.children = MenuForeach(list, one);
            }
            return oneList;
        }


        /// <summary>
        /// 根据级别获取所有权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<M_PageUrlInfo> GetMenuInfo()
        {
            using (M_PageUrlInfoDal dal = new M_PageUrlInfoDal())
            {
                int roleId = LoginHelper.GetRoleId();
                List<M_PageUrlInfo> list = dal.GetMenuInfo(roleId);
                List<M_PageUrlInfo> oneList = list.Where(x => x.LevelID == "1").ToList();
                foreach (var item in oneList)
                {
                    item.children = new List<M_PageUrlInfo>();
                    item.children = MenuForeach(list, item);
                }
                return oneList;
            }
        }

        /// <summary>
        /// 根据角色获取所有权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<M_PageUrlInfo> GetMenuInfoByRoleId(int RoleID)
        {
            using (M_PageUrlInfoDal dal = new M_PageUrlInfoDal())
            {
                return dal.GetMenuInfoByRoleId(RoleID);
            }
        }
      
        public List<M_PageUrlInfo> MenuForeach(List<M_PageUrlInfo> list, M_PageUrlInfo parent)
        {
            List<M_PageUrlInfo> newList = list.Where(x => x.LevelID == parent.ID).ToList();
            foreach (var item in newList)
            {
                item.children = new List<M_PageUrlInfo>();
                item.children.AddRange(MenuForeach(list, item));
            }
            return newList;
        }

        public List<TreeView> GetPageUrlInfoCombotree()
        {
            List<TreeView> tvList = new List<TreeView>();
            List<M_PageUrlInfo> pageUrlList = GetPageUrlInfoList();
            List<M_PageUrlInfo> oneList = pageUrlList.Where(x => x.LevelID == ((int)PageLevel.一级).ToString()).ToList();
            tvList.Add(new TreeView() { id = 1, text = "顶级", children = new List<TreeView>() });
            TreeView tv = null;
            foreach (var item in oneList)
            {
                tv = new TreeView()
                {
                    id = item.ID,
                    text = item.UrlName,
                    children = new List<TreeView>(),
                };
                Foreach(pageUrlList, item, tv);
                tvList[0].children.Add(tv);
            }
            return tvList;
        }



        private void Foreach(List<M_PageUrlInfo> pageUrlList, M_PageUrlInfo parent, TreeView tv)
        {
            List<M_PageUrlInfo> newList = pageUrlList.Where(x => x.LevelID == parent.ID).ToList();
            foreach (var item in newList)
            {
                TreeView newTv = new TreeView()
                {
                    id = item.ID,
                    text = item.UrlName,
                    children = new List<TreeView>(),
                };
                Foreach(pageUrlList, item, newTv);
                tv.children.Add(newTv);
            }
        }


        /// <summary>
        /// 获取页面路径列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<M_PageUrlInfo> GetPageUrlInfoList(int levelId)
        {
            using (M_PageUrlInfoDal dal = new M_PageUrlInfoDal())
            {
                return dal.GetPageUrlInfoList(string.Format("AND LevelID={0}", levelId));
            }
        }

        public List<M_PageUrlInfo> GetPageUrlInfoList()
        {
            return this.GetPageUrlInfoList(string.Empty);
        }


        /// <summary>
        /// 获取页面路径列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        private List<M_PageUrlInfo> GetPageUrlInfoList(string strWhere)
        {
            using (M_PageUrlInfoDal dal = new M_PageUrlInfoDal())
            {
                return dal.GetPageUrlInfoList(strWhere);
            }
        }

        public ReturnModel AddPageUrlInfo(M_PageUrlInfo model)
        {
            using (M_PageUrlInfoDal dal = new M_PageUrlInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Msg = "操作失败",
                };
                int res = dal.AddM_PageUrlInfo(model);
                if (res == 0)
                {
                    return r;
                }
                r.Msg = "操作成功";
                r.Success = true;
                return r;
            }
        }

        public ReturnModel DelPageUrlInfo(string ID)
        {
            using (M_PageUrlInfoDal dal = new M_PageUrlInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Msg = "操作失败",
                };
                int res = dal.DelM_PageUrlInfo(ID);
                if (res == 0)
                {
                    return r;
                }
                r.Msg = "操作成功";
                r.Success = true;
                return r;
            }
        }

        public ReturnModel EditPageUrlInfo(M_PageUrlInfo model)
        {
            using (M_PageUrlInfoDal dal = new M_PageUrlInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Msg = "操作失败",
                };
                int res = dal.EditM_PageUrlInfo(model);
                if (res == 0)
                {
                    return r;
                }
                r.Msg = "操作成功";
                r.Success = true;
                return r;
            }
        }

        public M_PageUrlInfo GetPageUrlInfoModel(string id)
        {
            using (M_PageUrlInfoDal dal = new M_PageUrlInfoDal())
            {
                return dal.GetM_PageUrlInfoModel(id);
            }
        }


        /// <summary>
        /// 获取角色页面所有权限 RolePageUrlInfo/RolePageUrlIndex 页面 
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<TreeView> GetPageUrlJson(int? roleId)
        {
            List<M_RolePageUrlInfo> rpuList = new M_RolePageUrlInfoBll().GetRolePageUrlInfoList(roleId);
            List<TreeView> tvList = new List<TreeView>();
            List<M_PageUrlInfo> pageUrlList = new M_PageUrlInfoBll().GetPageUrlInfoList();
            List<M_PageUrlInfo> oneList = pageUrlList.Where(x => x.LevelID == ((int)PageLevel.一级).ToString()).ToList();
            List<M_PageUrlInfo> twoList = null;
            TreeView tv = null;
            TreeView tvs = null;
            foreach (var item in oneList)
            {
                tv = new TreeView()
                {
                    id = item.ID,
                    text = item.UrlName,
                    attributes = new Attributes()
                    {
                        parentId = "0",
                    }
                };
                tv.children = new List<TreeView>();
                twoList = pageUrlList.Where(x => x.LevelID == item.ID).ToList();
                foreach (var two in twoList)
                {
                    tvs = new TreeView()
                    {
                        id = two.ID,
                        text = two.UrlName,
                        attributes = new Attributes()
                        {
                            parentId = item.ID
                        },
                        children = pageUrlList.Where(x => x.LevelID == two.ID).Select(x =>
                        new TreeView()
                        {
                            id = x.ID,
                            text = x.UrlName,
                            attributes = new Attributes() { parentId = string.Format("{0},{1}", two.ID, item.ID) },
                            @checked = rpuList.Exists(y => y.PageUrlID == x.ID) ? true : false
                        }).ToList(),
                    };
                    if (tvs.children.Count == 0)
                    {
                        tvs.@checked = rpuList.Exists(x => x.PageUrlID == two.ID) ? true : false;
                    }
                    tv.children.Add(tvs);
                }
                tvList.Add(tv);
            }
            return tvList;
        }
    }
}
